<?php

/**
 * Validate helper class.
 *
 * @version  $Id: validate.php 2 2009-10-02 23:06:43Z perfilev $
 * @package  System
 */
 
class Validate
{
	public static function alias($value)
	{
		return (bool) preg_match('/^[a-z0-9.-]+$/us', $value);
	}
	
	public static function date($value)
	{
		return (bool) preg_match('/^\d{4}-\d{2}-\d{2}$/us', $value);
	}

	public static function datetime($value)
	{
		if ((bool) preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/us', $value) || (bool) preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/us', $value)) {
			return true;
		} else {
			return false;
		}
	}

	public static function alpha($value)
	{
		return (bool) preg_match('/^[a-z_]+$/usi', $value);
	}

	public static function alphanum($value)
	{
		return (bool) preg_match('/^[a-z0-9_]+$/usi', $value);
	}

	public static function integer($value, $min = null, $max = null)
	{
		$options = array();
		if (null !== $min) {
			$options['min_range'] = $min;
		}
		if (null !== $max) {
			$options['max_range'] = $max;
		}
		return false !== filter_var($value, FILTER_VALIDATE_INT, array('options' => $options));
	}

	public static function float($value)
	{
		return false !== filter_var($value, FILTER_VALIDATE_FLOAT);
	}

	public static function ip($value)
	{
		return false !== filter_var($value, FILTER_VALIDATE_IP);
	}

	public static function email($value)
	{
		return false !== filter_var($value, FILTER_VALIDATE_EMAIL);
	}

	public static function url($value)
	{
		return false !== filter_var($value, FILTER_VALIDATE_URL);
	}

	/**
	 * Checks length of the given string.
	 *
	 *   Validate::length('test', 4);    // true
	 *   Validate::length('test', 3, 5); // true
	 *
	 * @return boolean
	 */
	public static function length($value)
	{
		$length = mb_strlen($value);
		if (func_num_args() == 2) {
			return $length == func_get_arg(1);
		}
		$firstArg = func_get_arg(1);
		$secondArg = func_get_arg(2);
		return self::integer($length, $firstArg, $secondArg);
	}
	
	/**
	 * Checks if (x)html page is valid.
	 *
	 * @param  string  $url  URL of page to be validated.
	 * @return boolean
	 */
	public static function page($url)
	{
		if (($url = filter_var($url, FILTER_VALIDATE_URL))) {
			try {
				$response = file_get_contents('http://validator.w3.org/check?uri=' . urlencode($url));
			} catch(Exception $e) {
				return false;
			}
			if (preg_match('|<title>(.*)</title>|is', $response, $matches)) {
				return (substr_count($matches[1], '[Valid]') > 0);
			}
		} else {
			throw new InvalidArgumentException('Invalid URL.');
		}
		return false;
	}

	/**
	 * Checks file extension.
	 *
	 * @param  string  $path        Path to file.
	 * @param  array   $extensions  Allowed extesions.
	 * @return boolean
	 */
	public static function fileext($path, array $extensions)
	{
		$basename = basename($path);
		if (false === $dot = strrpos($basename, '.')) {
			return false;
		}
		$ext = strtolower(substr($basename, $dot + 1));
		return in_array($ext, $extensions);
	}
}